package org.example.managementsystem.controller;


import org.example.managementsystem.model.Product_Plan;
import org.example.managementsystem.service.Product_PlanService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/product_plans")
@CrossOrigin(origins = "*")
public class Product_PlanController {
    private final Product_PlanService productPlanService;

    public Product_PlanController(Product_PlanService productPlanService) {
        this.productPlanService = productPlanService;
    }

    /**
     * 获取所有人员信息
     */
    @GetMapping
    public ResponseEntity<List<Product_Plan>> getAllProduct_Plan() {
        List<Product_Plan> product_plans = productPlanService.getAllProduct_Plans();
        return ResponseEntity.ok(product_plans);
    }

    /**
     * 根据ID获取人员信息
     */
    @GetMapping("/{plan_id}")
    public ResponseEntity<Product_Plan> getProduct_PlanById(@PathVariable Long plan_id) {
        Product_Plan product_plan = productPlanService.getProduct_PlanById(plan_id);
        if (product_plan != null) {
            return ResponseEntity.ok(product_plan);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 添加人员
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addProduct_Plan(@RequestBody Product_Plan product_plan) {
        Product_Plan savedProduct_Plan = productPlanService.addProduct_Plan(product_plan);
        Map<String, Object> response = new HashMap<>();
        response.put("plan_id", savedProduct_Plan.getPlan_id());
        response.put("message", "人员添加成功");
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新人员信息
     */
    @PutMapping("/{plan_id}")
    public ResponseEntity<Map<String, String>> updateProduct_Plan(@PathVariable Long plan_id, @RequestBody Product_Plan product_plan) {
        product_plan.setPlan_id(plan_id);
        boolean updated = productPlanService.updateProduct_Plan(product_plan);

        Map<String, String> response = new HashMap<>();
        if (updated) {
            response.put("message", "计划表更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "计划表更新失败，未找到指定ID的计划表");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 删除人员
     */
    @DeleteMapping("/{plan_id}")
    public ResponseEntity<Map<String, String>> deleteProduct_Plan(@PathVariable Long plan_id) {
        boolean deleted = productPlanService.deleteProduct_Plan(plan_id);

        Map<String, String> response = new HashMap<>();
        if (deleted) {
            response.put("message", "计划表删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "计划表删除失败，未找到指定ID的计划表");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    @GetMapping("/completion")
    public ResponseEntity<Map<String, Object>> getPlanCompletion() {
        Map<String, Object> completion = productPlanService.calculatePlanCompletion();
        return ResponseEntity.ok(completion);
    }
}
